package com.example.fragment;

import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager2.widget.ViewPager2;

import com.example.fragment.adapter.MyViewPagerAdapter;
import com.example.fragment.tab.DiscoveryFragment;
import com.example.fragment.tab.HomeFragment;
import com.example.fragment.tab.MyFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;

import java.util.ArrayList;
import java.util.List;

//不能滑动的底部导航Activity
public class BottomNavigationActivity1 extends AppCompatActivity {
    private BottomNavigationView mBottomNav;
    private List<Fragment> fragmentList;
    // 当前显示的fragment
    private int mPreFragmentFlag = 0;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置视图
        setContentView(R.layout.activity_fragment_bottom_navigation);
        //初始化view
        initView();
        //初始化Fragment相关配置
        initFragment();
        selectFragment();
    }

    private void initView() {
        //隐藏ActionBar
       getSupportActionBar().hide();
        //获取控件实例
        mBottomNav=findViewById(R.id.bnv);
    }

    private void initFragment() {
        fragmentList=   new ArrayList<>();
        //创建三个界面的Fragment
        HomeFragment homeFragment = new HomeFragment();
        DiscoveryFragment discoveryFragment = new DiscoveryFragment();
        MyFragment myFragment = new MyFragment();
        //添加到集合
        fragmentList.add(homeFragment);
        fragmentList.add(discoveryFragment);
        fragmentList.add(myFragment);
        //加载Fragment 到activity的占位符里
        //参数 用来显示fragment的占位符的id   显示的是fragment 这里默认0 即 HomeFragment
        initLoadFragment(R.id.container_id,0,fragmentList);
    }

    private void initLoadFragment(int containerId, int showFragment, List<Fragment> fragmentList) {
        //获取到FragmentManager实例的同时去开启事物
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        for (int i = 0; i < fragmentList.size(); i++) {
            //首先将Fragment添加到事务中
            fragmentTransaction.add(containerId,fragmentList.get(i),fragmentList.get(i).getClass().getName());
            //默认展示 fragmentList.get(showFragment)
            //这里做首次Fragment的展示，如果不是指定的Fragment就先隐藏，需要的时候再显示出来
            if (i!=showFragment){
                fragmentTransaction.hide(fragmentList.get(i));
            }
        }
        //提交事务
        fragmentTransaction.commit();

    }


    private void selectFragment() {
        //设置icon的颜色
       // mBottomNav.setItemIconTintList(null);
        //注册监听事件
        mBottomNav.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                //获取item对象的id
                int itemId = item.getItemId();
                switch (itemId) {
                    case R.id.home:
                        //操作显示和隐藏fragment的方法  参数： 1： 要显示的fragment  2： 要隐藏的fragment即当前显示的fragment
                        showAndHideFragment(fragmentList.get(0), fragmentList.get(mPreFragmentFlag));
                        mPreFragmentFlag = 0;
                        break;
                    case R.id.discovery:
                        showAndHideFragment(fragmentList.get(1), fragmentList.get(mPreFragmentFlag));
                        mPreFragmentFlag = 1;
                        break;
                    case R.id.my:
                        showAndHideFragment(fragmentList.get(2), fragmentList.get(mPreFragmentFlag));
                        mPreFragmentFlag = 2;
                        break;
                }
                return true;
            }
        });


    }

    //加载不同的Fragment
    private void showAndHideFragment(Fragment show, Fragment hide) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (show != hide){
            //显示选择的fragment
            transaction.show(show);
            //隐藏当前显示fragment
            transaction.hide(hide);
            //.commitAllowingStateLoss();
            transaction.commit();
        }


    }
}

